Introduction To C#

What is C#?

C#, pronounced as C Sharp, is an object-oriented programming language developed by Microsoft in the early 2000s, led by Anders Hejlsberg. It is part of the .Net framework and is intended to be a simple general-purpose programming language that can be used to develop different types of applications, including console, windows, web, mobile applications, games.

Basic C# code structure

using System;
namespace HelloWorld {
	class Program {
		static void Main(string[] args) {
			Console.WriteLine("Hello, World!"); // Used to printing
			Console.Read(); // Used for taking inputs
		}
	}
}

3.1 Basic Terminologies

Directive

This tells the compiler that our program uses a certain namespace.

using System;

this directive tells the compiler that our program uses System namespace

Namespaces

C# has a large amount of pre-written code that are organised into different namespaces. The System namespace contains code for methods that allow us to do certain functions. We can also declare our own namespaces.

One advantage of namespaces is that we can prevent name conflicts. 2 or more codes can have the same name as long as they belong to different namespaces. Let's look at a example.

using System;
namespace Example1 {
	class Program {
		static void Main(string[] args) {
			Console.WriteLine("Hello, World 1!");
		}
	}
}

namespace Example2 {
	class Program {

	}
}

But only one of these can have Main method, because the it is the main starting point of our program and if both the namespaces contains Main method then the compiler won't understand which one to execute.

Main() method

This Main() method is the starting point of any C# program. Whenever a C# program is executed the Main() method is the first to get called. The "string[] args" inside the () parenthesis of our Main() method can take an array of strings as input.

The Main() method contains two lines of code. The first line

Console.WriteLine("Hello, World!");

displays the line "Hello, World!" on the screen.

Console.Read();

waits for a key press from the user before closing the console execution.